// pages/views/cardshow/cardshow.js
const utils = require('../../../utils/util.js');
const root = getApp().globalData.ROOT_URL;
import {closeAdd,addBtn} from '../../../utils/imgbase64.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //回复类型
    posttype:'1',
    //引用信息
    pid:'',
    be_postnkname:'',
    be_postcontent:'',
    // 文本val
    textval: '',
    //添加图片数组
    imgArr:[],
    // 数字最大
    maxtext: 500,
    // 从0开始
    nowtext: 0,
    //按钮状态
    submitdis:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.gid = options.gid;
     this.setData({
         posttype:options.posttype,
         closeAdd,
         addBtn
     })
     if(options.posttype=='0'){
         this.setData({
             posttype:options.posttype,
             pid: options.pid,
             be_postnkname:options.pnkname,
             be_postcontent:options.pcontent
         })
     }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let app = getApp();
    app.loginState();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
   
  },
  //文本数字计算
  bindTextAreaBlur: function (e) {
    let val = e.detail.value, len = parseInt(val.length);
    if (len <= this.data.maxtext){
        this.setData({
            textval:val,
            nowtext:len
        })
    }
  },
  // 选择图片
 addimg:function () {
    let con = 3 - this.data.imgArr.length;
    wx.chooseImage({
      count: con,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        console.log(res)
        let tempFilePaths = res.tempFilePaths;
        this.data.imgArr = this.data.imgArr.concat(tempFilePaths);
        this.setData({
            imgArr: this.data.imgArr
        })
      }
    })
  },
  //更换图片
  changeimg:function(e){
      let index = e.target.dataset.index;
      wx.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: res => {
              let tempFilePaths = res.tempFilePaths[0];
              this.data.imgArr.splice(index,1,tempFilePaths);
              this.setData({
                  imgArr: this.data.imgArr
              })
          }
      })
  },
  //删除图片
  removeimg:function(e){
    let index = e.target.dataset.index;
    this.data.imgArr.splice(index,1);
    this.setData({
        imgArr: this.data.imgArr
    })
  },
  postMsg:function(e){
    if (!utils.empty(this.data.textval)){
        wx.showToast({
            title:'请输入您想发表的信息~',
            icon:'none'
        })
    }else{
        this.setData({
            submitdis: true
        })
        wx.showLoading({
            title: '发送中',
        })
        utils.ajax(root + 'groups/' + this.gid + '/replies', {
            header: {
                "Accept": 'application/vnd.dct.v1+json'
            },
            data: {
                uid: wx.getStorageSync("user_info").uid,
                pid: this.data.pid,
                content: this.data.textval
            },
            method: 'post'
        }, res => {
            wx.hideLoading();
            if(res.state=="1"){
                let files_id = res.cid;
                let token = res.token;
                if(this.data.imgArr.length>0){
                    this.uploadimg(files_id, this.data.imgArr, 0, this.data.imgArr.length,token);
                }else{
                    this.setData({
                        submitdis: false
                    })
                    wx.navigateBack();
                }
            }else{
                this.setData({
                    submitdis: false
                })
                wx.showToast({
                    title:'发送失败，请重试~',
                    icon:'none'
                })
            }
        }, err => {
            this.setDat({
                submitdis: false
            })
            wx.hideLoading();
            wx.showToast({
                title: '发送失败，网络错误~',
                icon: 'none'
            })
        })
    }
  },
  uploadimg:function(fid,arr,num,length,token){
      wx.uploadFile({
          url: root + 'replies/' + fid + '/file',
          filePath: arr[num],
          name: 'file',
          formData:{
              token:token
          },
          complete:res=>{
              num++;
              if(num == length){
                  wx.hideLoading();
                  this.setData({
                      submitdis: false
                  })
                  wx.navigateBack();
              }else{
                  this.uploadimg(fid, arr, num, length,token);
              }
          }
      })
  },
})